/*
A socket client that sends a JSON message to an SMTP server using the SMTP protocol
*/
var net = require('net');

function SmtpClient(port, host) {

    var _this = this;
    var socket = this._socket = new net.Socket();
    socket.connect(port, host, function () {
        console.log('SMTP connected');
        socket.on('data', function (data) {
            _this._onData(data.toString());
            //socket.destroy();
        });
    });


    socket.on('close', function () {
        console.log('SMTP connection closed');
    });

}

SmtpClient.prototype = {
    sendMessage: function (msg) {
        this._onData = function (data) {
            if (data.indexOf("250 ") !== 0) throw new Error("Unexpected response: " + data);
        }
        this._socket.write("HELO");
    },

    _onData: function (data) {
        if (data.indexOf("220 ") !== 0) throw new Error("Unexpected response: " + data);
    }
}

module.exports = SmtpClient;